/*
 * Created on Oct 9, 2003
 * LanguageDB
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.DBWrapable;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Language;
import ro.gateway.aida.utils.LocalisedList;


/**
 * @author Mihai Postelnicu
 * LanguageDB
 * Accesses languages that are registered in the database.
 *  *
 */
public class LanguageDB extends DBPersistenceManager implements DBWrapable {
   
    private LanguageDB(PersistenceToken token) {
        super(token);
    }

    public static LanguageDB getManager(PersistenceToken token) {
        return new LanguageDB(token);
    }

    /**
     * Gets the default language abbrev
     * @return the default language abbrev
     * @throws SQLException
     */
    public String getDefault() throws SQLException {
        Connection con = getConnection();

        try {
            PreparedStatement ps = con.prepareStatement(GET_DEFAULT);
            ResultSet rs = ps.executeQuery();

            if (rs.next())
                return rs.getString(1);

            rs.close();
            ps.close();
        } finally {
            con.close();
        }

        return null;
    }
    
	public String setDefault(String lang) throws SQLException {
		   Connection con = getConnection();

		   try {
			   PreparedStatement ps = con.prepareStatement(SET_DEFAULT);
			   ps.setString(1,lang);
			   ps.executeUpdate();

			   ps.close();
		   } finally {
			   con.close();
		   }

		   try {
			   PreparedStatement ps = con.prepareStatement(SET_TRN_DEFAULT);
			   ps.setString(1,lang);
			   ps.executeUpdate();

			   ps.close();
		   } finally {
			   con.close();
		   }

		   
		   return null;
	   }

    /**
     * Gets a list with all available languages, multistrings
     * @return the list
     * @throws SQLException
     */
    public LocalisedList getAll() throws SQLException {
        LocalisedList v = new LocalisedList();
        Connection con = getConnection();

        //TODO: sa fac un alt tabel pt limbi, don't like this!!
        try {
            PreparedStatement ps = con.prepareStatement(GET_ALL);

            ResultSet rs = ps.executeQuery();
            String clng = null;
            Language l = null;
            rs.next(); //ignora prima linie, nu ne intereseaza

            while (rs.next()) {
                if (!rs.getString(1).equals(clng)) {
                    if (l != null)
                        v.add(l);

                    l = new Language();
                    clng = rs.getString(1);
                }

                l.setAbbrev(rs.getString(1).substring(10, 12));
                String langt=rs.getString(2);
                String namet=rs.getString(3);
                l.getName().setString(langt,namet);
            }
            v.add(l);

            rs.close();
            ps.close();
        } finally {
            con.close();
        }

        return v;
    }

	public void delete(Object o) throws Exception {
	}
	  
	public Object get(Object o) throws Exception {
		return getAll();
	}
	
	public void synchronize(Object o) throws Exception {
		
	}
	
	private static final String SET_DEFAULT =
			"UPDATE system_properties SET value=? WHERE name='defaultLang'";
	
	private static final String SET_TRN_DEFAULT =
		"UPDATE iface_trans set text=? WHERE module='translator' AND " +
		"label='lng.default'";

	
    private static final String GET_DEFAULT =
        "SELECT value FROM system_properties WHERE name='defaultLang'";
    private static final String GET_ALL =
        "SELECT label,lang,text FROM iface_trans WHERE " +
        "module='translator' AND label ILIKE('lng.avail%') ORDER BY label";
}
